package com.cjhme.system.impl.service.position.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cjhme.common.model.base.CascadeTreeNodeBean;
import com.cjhme.common.model.base.DataPaging;
import com.cjhme.common.model.base.TreeNodeBean;
import com.cjhme.common.model.position.PositionBean;
import com.cjhme.common.util.CreateTreeUtil;
import com.cjhme.system.impl.dao.position.PositionDao;
import com.cjhme.system.impl.service.common.impl.CascadeTreeServiceImpl;
import com.cjhme.system.impl.service.position.PositionService;

/**
*
* <p>
* Title: PositionDaoImpl.java
* </p>
* Description: 岗位
* <p>
* Modify histoty:
*
* @author cjh
* @version 1.0
*/
@Service("system.positionService")
public class PositionServiceImpl extends CascadeTreeServiceImpl<CascadeTreeNodeBean<String>, String> implements PositionService {
	
	private PositionDao positionDao;

	/**
	 * 查询所有岗位结构 
	 * @return
	 */
	public String queryPositionList(String rootState, String childState)throws Exception {
		List<TreeNodeBean> list = this.positionDao.queryPositionList();
		return CreateTreeUtil.getTreeJson(list, "", rootState, childState);
	}
	
	/**
	 * 根据条件查询岗位
	 * @param parameter
	 * @return
	 */
	public List<TreeNodeBean> queryPositionByCondition(Map<String,Object> parameter)throws Exception {
		return this.positionDao.queryPositionByCondition(parameter);
	}
	
	/**
	 * 根据条件分页查询岗位
	 * @param parameter
	 * @return
	 */
	public DataPaging<Object> queryPositionByConditionPaging(DataPaging<Object> pageParameter) {
		return this.positionDao.queryPositionByConditionPaging(pageParameter);
	}
	
	/**
	 * 判断岗位是否存在
	 * @param parameter
	 * @return
	 */
	public int queryPositionIsExists(Map<String,Object> parameter) {
		return this.positionDao.queryPositionIsExists(parameter);
	}
	
	/**
	 * 添加岗位
	 * @param parameter
	 * @return
	 */
	@Transactional
	public int savePosition(Map<String,Object> parameter) {
		Map<String, Object> tableParameter = getTableParameter();
		CascadeTreeNodeBean<String> cascadeTreeNodeBean = new CascadeTreeNodeBean<>();
		cascadeTreeNodeBean.setId(String.valueOf(parameter.get("positionId")));
		cascadeTreeNodeBean.setParentId(String.valueOf(parameter.get("parentPositionId")));
		addLast(tableParameter, cascadeTreeNodeBean);

		parameter.put("rightValue", cascadeTreeNodeBean.getRightValue());
		parameter.put("leftValue", cascadeTreeNodeBean.getLeftValue());
		return this.positionDao.savePosition(parameter);
	}
	
	/**
	 * 根据id查询岗位 
	 * @param parameter
	 * @return
	 */
	public PositionBean queryPositionByPositionId(Map<String,Object> parameter) {
		return this.positionDao.queryPositionByPositionId(parameter);
	}
	
	/**
	 * 根据id修改岗位
	 * @param parameter
	 * @return
	 */
	@Transactional
	public int updatePositionByPositionId(Map<String,Object> parameter) {
		String parentPositionId = String.valueOf(parameter.get("parentPositionId"));
		PositionBean oldPosition = this.positionDao.queryPositionByPositionId(parameter);

		if ((!"null".equals(parentPositionId) && oldPosition.getParentPositionId() != null && !oldPosition.getParentPositionId().equals(parentPositionId))
				|| (!"null".equals(parentPositionId) && !parentPositionId.equals(oldPosition.getParentPositionId()))) {
			Map<String, Object> tableParameter = getTableParameter();
			shiftLayer(tableParameter, String.valueOf(parameter.get("positionId")), parentPositionId);
		}


		return this.positionDao.updatePositionByPositionId(parameter);
	}
	
	/**
	 * 根据ids删除岗位
	 * @param parameter
	 * @return
	 */
	@Transactional
	public int deletePositionByIds(Map<String,Object> parameter) {
		// 删除岗位用户映射
		this.positionDao.deletePositionUserMappingByIds(parameter);

		// 删除岗位
		this.positionDao.deletePositionByIds(parameter);
		return 1;
	}
	
	
	/**
	 * 根据id移动岗位
	 * @param parameter
	 * @return
	 */
	@Transactional
	public int updatePositionParentPositionIdById(Map<String,Object> parameter) {
		return this.positionDao.updatePositionParentPositionIdById(parameter);
	}
	
	/**
	 * 根据组织ids删除岗位用户映射
	 * @param parameter
	 * @return
	 */
	@Transactional
	public int deletePositionUserMappingByIds(Map<String,Object> parameter) {
		return this.positionDao.deletePositionUserMappingByIds(parameter);
	}
	
	private Map<String, Object> getTableParameter() {
		Map<String, Object> tableParameter = new HashMap<>();
		tableParameter.put("tableName", "T_POSITION");
		tableParameter.put("idCol", "POSITION_ID");
		tableParameter.put("parentIdCol", "PARENT_POSITION_ID");
		tableParameter.put("leftCol", "LEFT_VALUE");
		tableParameter.put("rightCol", "RIGHT_VALUE");
		tableParameter.put("layerCol", "POSITION_LEVEL");

		return tableParameter;
	}

	public PositionDao getPositionDao() {
		return positionDao;
	}

	@Resource(name="system.positionDao")
	public void setPositionDao(PositionDao positionDao) {
		this.positionDao = positionDao;
	}
	
	
}
